/*
 * Listen.java
 *
 * Created on May 13, 2009, 5:17 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package im.general;

import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

/**
 *
 * @author bluejack
 */
public abstract class AbstractListenBehaviour extends CyclicBehaviour {

    //TypeBehavioursHandler bhandler;
    protected MessageTemplate mt;

    public void action() {
        ACLMessage msg;
        
        if(mt==null) {
            msg = myAgent.receive();
        }
        else {
            msg = myAgent.receive(mt);
        }

        if (msg != null) {
            //System.out.println("["+this.getClass().getName()+"]" + msg.getEncoding());
            if(Config.TRACE_LVL > Config.NIGHTMARE_TR_LVL )
                System.out.println("[AbstractListenBehaviour] " + myAgent.getAID().getName() + " handling message performative: "+msg.getPerformative());
            handleMessage(msg);
        }
        else {
            //System.out.println("["+this.getClass().getName()+"] NULL MESSAGE");
            this.block(1000);
        }
    }

    protected MessageTemplate setTemplate(int t) {
        return MessageTemplate.MatchPerformative(t);
    }
    
    protected abstract void handleMessage(ACLMessage msg);
}
